export default function initShader(gl,vertexShaderSrouce,fragementShaderSource){
	 //调用创建shader的函数 
	let vertexSahder=createShader(gl,gl.VERTEX_SHADER,vertexShaderSrouce); 
	let fragementShader=createShader(gl,gl.FRAGMENT_SHADER,fragementShaderSource);
	let program=createProgram(gl,vertexSahder,fragementShader); 
	if(program){
		//使用哪个应用程序
		gl.useProgram(program); 
		gl.program=program
		return true;
	}else{ 
		return false;
	}
}


function createShader(gl,type,source){
	  let shader=gl.createShader(type);
	  //将shader渲染到指定的shader容器内
	  gl.shaderSource(shader,source);
	  //编译shaer
	  gl.compileShader(shader); 
	  //捕获shader编译异常的情况 
	  let compiled=gl.getShaderParameter(shader,gl.COMPILE_STATUS);
	  if(compiled){
		  return shader;
	  }else{
	  	 //获取编译的错误信息
	  	let err=gl.getShaderInfoLog(shader);
	  	console.log("编译错误",+err);
	  	//虽然编译错误，但是还是创建了一个shader为了节省显存，需要删除没用的shader
	  	gl.deleteShader(shader);
	  	return null;
	  } 
 }  
		 
function createProgram(gl,shader,shader2){  
	//运行程序
	let program=gl.createProgram();
	if(!program) return null;
	//指定应用程序加载指定的shader
	gl.attachShader(program,shader); 
	gl.attachShader(program,shader2); 
	//连接到那个应用程序
	gl.linkProgram(program);
	let linked=gl.getProgramParameter(program,gl.LINK_STATUS);
	if(linked){ 
		return program; 
	}else{
	   let err=gl.getProgramInfoLog(program);
	   console.log("获取应用程序失败",err);
	   gl.deleteProgram(program);
	   gl.deleteShader(shader); 
	   gl.deleteShader(shader2); 
	   return null; 
	} 
}; 
	 
